package bdddaosimpl;

import java.util.Date;
import java.util.List;

import bdd.beans.*;
import bdddaos.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Set;

public class DAOExamenImpl implements DAOExamen {
    private DBLink dblink;

    public DAOExamenImpl(){
		this.dblink = new DBLink();
		this.dblink.connexion();
    }
    


	public Examen save(Examen o) {
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "INSERT INTO EXAMEN (LIBELLE, HORAIREDEBUT, HORAIREFIN, IDDATE, NUMTYPE)" +
			" VALUES ('"+o.getLibelle()+ "', '"+ o.getHorairedebut() +"', '"+o.getHorairefin() + "', '" + format.format(o.getJours().getIddate())+"', '" + o.getType().getNumtype()+"')";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			o.setNumexamen(outil.findMaxId("EXAMEN", "NUMEXAMEN"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requête : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	public Examen find(Integer id) {
		Examen retour = new Examen();
		retour.setNumexamen(id);
		try {
			this.dblink.connexion();
			String r = "SELECT * FROM EXAMEN WHERE NUMEXAMEN = " + id;
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				retour.setLibelle(rs.getString("LIBELLE"));
				retour.setHorairedebut(rs.getInt("HORAIREDEBUT"));
				retour.setHorairefin(rs.getInt("HORAIREFIN"));
				retour.setJours(new DAOJoursImpl().find(rs.getDate("IDDATE")));
				retour.setType(new DAOTypeImpl().find(rs.getInt("NUMTYPE")));
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requête" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	public List<Examen> findAll() {
		List<Examen> Lstretour = new ArrayList<Examen>();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM EXAMEN"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Examen retour = new Examen();
				retour.setLibelle(rs.getString("LIBELLE"));
				retour.setHorairedebut(rs.getInt("HORAIREDEBUT"));
				retour.setHorairefin(rs.getInt("HORAIREFIN"));
				retour.setJours(new DAOJoursImpl().find(rs.getDate("IDDATE")));
				retour.setType(new DAOTypeImpl().find(rs.getInt("NUMTYPE")));
				retour.setNumexamen(rs.getInt("NUMEXAMEN"));

				Lstretour.add(retour);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requête" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	public void update(Examen o) {
		try {
			this.dblink.connexion();
			DateFormat format = new SimpleDateFormat("dd-MM-yy");
			String r = "UPDATE EXAMEN SET LIBELLE='"+o.getLibelle()+ "', HORAIREDEBUT='"+o.getHorairedebut()+ "', HORAIREFIN='"+o.getHorairefin()+ "', NUMTYPE='"+o.getType().getNumtype()+ "', IDDATE='"+format.format(o.getJours().getIddate())+ "' WHERE NUMEXAMEN="+o.getNumexamen();

			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requête : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	public void delete(Examen o) {
		try {
			this.dblink.connexion();
			String r = "DELETE FROM EXAMEN WHERE NUMEXAMEN="+o.getNumexamen();

			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requête : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

      public  List<Examen> recupererListeExamenEns(int numEC, int promotion, int enseignant){
                List<Examen> Lstretour = new ArrayList<Examen>();
		try {
                    String rq = "select distinct libelle, examen.numexamen "
                        + "from JNTEXAMENPROMOTIONEC, CRENEAU, EXAMEN "
                        + "where creneau.numec=JNTEXAMENPROMOTIONEC.NUMEC and "
                        + "JNTEXAMENPROMOTIONEC.NUMEXAMEN=EXAMEN.NUMEXAMEN and "
                        + "creneau.numenseignant=" + enseignant
                        + " and creneau.numec=" + numEC
                        + " and creneau.numpromotion=" + promotion;
                
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(rq));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Examen retour = new Examen();
				retour.setLibelle(rs.getString("LIBELLE"));
				retour.setNumexamen(rs.getInt("NUMEXAMEN"));
				Lstretour.add(retour);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requête" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
    }

    public Set<Examen> findByJour(Date jour) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Set<Examen> findByType(Integer numType) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
